package cn.xmstore.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： aeolia
 * @Description： TODO Token工具类
 * @Date： 2021/8/20 14:23
 */
public class JWTUtil {

    static final private String HEADER_TYP = "JWT";
    static final private String HEADER_ALG = "HS256";
    static final private String SECRET = "BraveApes";
    static final private String ISSUER = "勇敢牛牛不怕困难";


    public static String generateToken(int id, String name) {
        //创建token的header
        Map<String,Object> header=new HashMap<>();
        header.put("typ",HEADER_TYP);
        header.put("alg",HEADER_ALG);

        //创建发行与过期时间
        long nowMillis = System.currentTimeMillis();
        Date iat = new Date(nowMillis);
        Date exp = new Date(nowMillis + 1000 * 10);

        //创建Claims存放用户信息
        Map<String,Object> claims=new HashMap<>();
        claims.put("id",id);
        claims.put("name",name);

        String token = JWT.create()
                .withHeader(header)
                .withIssuer(ISSUER)
                .withIssuedAt(iat)
                .withExpiresAt(exp)
                .withClaim("claims", claims)
                .sign(Algorithm.HMAC256(SECRET));

        return token;
    }

    public static int verify(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT decodedJWT = verifier.verify(token);
            long exp = decodedJWT.getExpiresAt().getTime();
            long iat = decodedJWT.getIssuedAt().getTime();
            long now = System.currentTimeMillis();
            long timeout=now-exp;
            if (timeout<0){//未超时
                return 200;
            }else if (timeout>0&&timeout<iat){//超时可刷新
                return 201;
            }else {//超时不可刷新
                return 301;
            }
        } catch (IllegalArgumentException e) {
            return 300;
        } catch (JWTVerificationException e) {
            return 300;
        }
    }

    public static Map<String,Object> getInfo(String token){
        DecodedJWT decodedJWT = null;
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            decodedJWT = verifier.verify(token);
            return decodedJWT.getClaim("claims").asMap();
        } catch (IllegalArgumentException e) {
            return null;
        } catch (JWTVerificationException e) {
            return null;
        }
    }


}


